package com.Util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DESCRIPTION: 用来管理时间格式转换
 *
 * @Author 韩武洽
 * @Date 2016-09
 * @Time 12 14:03
 **/
public class DateUtils {

    public static String parse(Date date, String patten) {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        result = sdf.format(date);
        return result;
    }

    public static String getBeforeDay(Date date,Integer dayNum) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cal.setTime(date);
        cal.add(Calendar.DATE, dayNum);
        return format.format(cal.getTime());

    }

    public static String getBeforeMonth(Date date,Integer beforeMonth) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        cal.setTime(date);
        cal.add(Calendar.MONTH, beforeMonth);
        return format.format(cal.getTime());
    }

    /**
     * @Description:
     * @Return: 格式化时间
     * @Author: 韩武洽 @Wyshown
     * @Date: 2017/3/28 11:45
     * @Version 2.0
     */
    public static String getAfterMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return format.format(cal.getTime());
    }

    /**
    * @DESCRIPTION:
    * @Return: 返回Timestamp的当前时间
    * @Author: 韩武洽 @Wyshown
    * @Date: 2016/9/12-14:04
    * @version V2.0
    **/
    public static Timestamp millisecondChangeTimestamp(){
        return millisecondChangeTimestamp(System.currentTimeMillis());
    }

    /**
    * @DESCRIPTION:
    * @Return: 把long转化为Timestamp的时间
    * @param time Long类型的毫秒数
    * @Author: 韩武洽 @Wyshown
    * @Date: 2016/9/12-14:06
    * @version V2.0
    **/
    public static Timestamp millisecondChangeTimestamp(Long time){
        return new Timestamp(time);
    }

    /**
     * @DESCRIPTION:
     * @Return: 把Date转化为Timestamp的时间
     * @param tsStr Date类型的毫秒数
     * @Author: 韩武洽 @Wyshown
     * @Date: 2016/9/12-14:06
     * @version V2.0
     **/
    public static Timestamp dateChangeTimestamp(String tsStr){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(tsStr);
            System.out.println(ts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }

    /**
     * String(yyyy-MM-dd HH:mm:ss)转10位时间戳
     * @param time
     * @return
     */
    public static Integer StringToTimestamp(String time){
        int times = 0;
        try {
            times = (int) ((Timestamp.valueOf(time).getTime())/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(times==0){
            System.out.println("String转10位时间戳失败");
        }
        return times;
    }

    /**
     * String(yyyy-MM-dd HH:mm:ss) 转 Date
     *
     * @param time
     * @return
     * @throws ParseException
     */
    // String date = "2010/05/04 12:34:23";
    public static Date StringToDate(String time) throws ParseException {

        Date date = new Date();
        // 注意format的格式要与日期String的格式相匹配
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(time);
            System.out.println(date.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return date;
    }

    /**
     * Date转为String(yyyy-MM-dd HH:mm:ss)
     *
     * @param time
     * @return
     */
    public static String DateToString(Date time) {
        String dateStr = "";
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            dateStr = dateFormat.format(time);
            System.out.println(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss)
     * @param time
     * @return
     */
    public static String timestampToString(Integer time){
        //int转long时，先进行转型再进行计算，否则会是计算结束后在转型
        long temp = (long)time*1000;
        Timestamp ts = new Timestamp(temp);
        String tsStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //方法一
            tsStr = dateFormat.format(ts);
            System.out.println(tsStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tsStr;
    }

    /**
     * 10位时间戳转Date
     * @param time
     * @return
     */
    public static Date TimestampToDate(Integer time){
        long temp = (long)time*1000;
        Timestamp ts = new Timestamp(temp);
        Date date = new Date();
        try {
            date = ts;
            //System.out.println(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date类型转换为10位时间戳
     * @param time
     * @return
     */
    public static Integer DateToTimestamp(Date time){
        Timestamp ts = new Timestamp(time.getTime());

        return (int) ((ts.getTime())/1000);
    }

    public static void main(String arg[]){
        int a = Integer.parseInt(String.valueOf(System.currentTimeMillis() / 1000 + 86400 * 30));
        System.out.println(a);
//        System.out.println(DateUtils.parse(new Date(),"yyyy-MM-dd hh:mm:ss"));
//        System.out.println(Timestamp.valueOf(DateUtils.parse(new Date(),"yyyy-MM-dd hh:mm:ss")));
//        parse(new Date(),"yyyy-MM-dd");
//        System.out.println(parse(new Date(),"yyyy-MM-dd"));
//        getBeforeDay(new Date(),10);
//        System.out.println(getBeforeDay(new Date(),10));

//        Date date = new Date();
//        Long time = date.getTime();
//        System.out.println(time);
//
//        Date d = new Date(time);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(sdf.format(d));
    }


}
